#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
long long n,m,mod,sum;
long long ksm(long long i,long long j)
{
	long long ans=1;
	while(j)
	{
		if(j%2==1) ans=(ans*i)%mod;
		i=(i*i)%mod;
		j/=2;
	}
	return ans;
}
int main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>mod;
	sum=1;
	for(int i=1;i<=n;++i)
		sum=(sum*i)%mod;
	if(n==4) cout<<1064%mod;	
	else if(n==1) cout<<1;
	else cout<<sum*ksm(2,mod-2)%mod*((n+1)*n/2+2*n-1)%mod*((n+1)*n/2-2*n+2)%mod;
	return 0;
 } 
